﻿using System;
using Furiant.Figment.Agents.Inventory;
using Furiant.Figment.Core;

namespace Furiant.Figment.Items
{
    public static class RechargeableExtensions
    {
        /// <summary>
        /// Invokes an attempt by the specified Entity to increase the Charge value of this instance using its Func&lt;Entity, ushort&gt; RestoreFunction.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// The resulting value is clamped to the ChargeMax value of the instance.
        /// </summary>
        /// <param name="owner">The Entity whose members are used to compute the result of the RestoreFunction.</param>
        public static void Restore(this IRechargeable source, IHasInventory owner)
        {
            if (source.Charge == source.ChargeMax) return;

            var result = source.RestoreFunction(owner);
            source.Charge = (ushort)Utility.ClampNumber(0, source.ChargeMax, source.Charge + result);
        }

        /// <summary>
        /// Invokes an attempt by the specified Entity to reduce the Charge value of this instance using its Func&lt;Entity, ushort&gt; DegradeFunction.
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// </summary>
        /// <param name="owner">The Entity whose members are used to compute the result of the DegradeFunction.</param>
        public static void Deplete(this IRechargeable source, IHasInventory owner)
        {
            if (source.Charge == 0) return;

            var result = source.DepleteFunction(owner);
            source.Charge = (ushort)Math.Max(0, source.Charge - result);
        }
    }
}
